﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Configuration;
using System.Diagnostics;

namespace PatientsManagement
{
    public static class HelperMethods
    {
        public static string HashPassword(string password)
        {
            HashAlgorithm SHA256 = new SHA256CryptoServiceProvider();
            Byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            Byte[] salt = Encoding.UTF8.GetBytes("Qh1KlO9jPa");
            Byte[] saltedPassword = new Byte[salt.Length + passwordBytes.Length];
            salt.CopyTo(saltedPassword, 0);
            passwordBytes.CopyTo(saltedPassword, salt.Length);
            Byte[] hashedBytes = SHA256.ComputeHash(saltedPassword);
            return BitConverter.ToString(hashedBytes);
        }

        public static void RSA(bool encrypt = true)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
                if (encrypt && !section.SectionInformation.IsProtected)
                {
                    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                }
                else if (!encrypt && section.SectionInformation.IsProtected)
                    section.SectionInformation.UnprotectSection();
                config.Save();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                if(encrypt)
                    MessageBox.Show("Zaszyfrowanie pliku konfiguracyjnego nie powiodło się.");
                else
                    MessageBox.Show("Odszyfrowanie pliku konfiguracyjnego nie powiodło się.");
            }
        }
    }
}
